package com.example.order.controller;

import com.example.order.entity.OrderEntity;
import com.example.order.service.RiderOrderService;
import com.example.order.service.UserOrderService;
import com.example.order.service.impl.OrderQueueService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : zhayh
 * @date : 2021-5-8 20:54
 * @description : 订单服务API
 */

@RestController
@RequestMapping("/api/orders")
public class OrderController {
    @Resource
    private UserOrderService userOrderService;
    @Resource
    private OrderQueueService orderQueueService;
    @Resource
    private RiderOrderService riderOrderService;

    // 用户下单
    @PostMapping("/create")
    public ResponseEntity<OrderEntity> createOrder(String goodsName, Long goodsCount,
                                      Long goodsPrice, Long totalPrice, String user) {
        OrderEntity result = userOrderService.createOrder(goodsName, goodsCount, goodsPrice, totalPrice, user);
        return ResponseEntity.ok(result);
    }

    // 用户查询订单
    @GetMapping("/mine")
    public ResponseEntity<List<OrderEntity>> getOrders(String user) {
        List<OrderEntity> result = userOrderService.getMyOrders(user);
        return ResponseEntity.ok(result);
    }

    // 骑手获取一条配送订单
    @GetMapping("/waiting-deliver")
    public ResponseEntity<Object> getDeliverOrder() {
        String orderCode = orderQueueService.receiveDeliverOrder();
        if (orderCode == null || orderCode.isEmpty()) {
            return ResponseEntity.ok("暂无待配送订单");
        }
        OrderEntity orderEntity = riderOrderService.confirmSend(orderCode);
        return ResponseEntity.ok(orderEntity);
    }

    //  骑手完成配送
    @PostMapping("/finish-deliver")
    public ResponseEntity<OrderEntity> finishDeliver(String orderCode) {
        OrderEntity result = riderOrderService.finishDeliver(orderCode);
        return ResponseEntity.ok(result);
    }
}
